using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
    /*
      var qGSDAT = from m in mtemp.GSDAT select m;
       DBMapper.MapGSDAT(qGSDAT.ToList(), true);
       */
        public List<GSDAT> MapGSDAT(XElement item2, bool mergedb)
        {
            List<GSDAT> result = new List<GSDAT>();
            var item4 = from item3 in item2.Elements()
                        select new GSDAT
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            GSMANDANT = (string)item3.Element("GSMANDANT"),
                            GSART = (string)item3.Element("GSART"),
                            GSVAR = (string)item3.Element("GSVAR"),
                            GSHANDLNR = (string)item3.Element("GSHANDLNR"),
                            GSGARNBEZ = (string)item3.Element("GSGARNBEZ"),
                            GSNUMART = (string)item3.Element("GSNUMART"),
                            GSGARNNR = (string)item3.Element("GSGARNNR"),
                            GSFACH = (string)item3.Element("GSFACH"),
                            GSRECHNUM = (string)item3.Element("GSRECHNUM"),
                            GSRECHNR = (string)item3.Element("GSRECHNR"),
                            PHOTO = (string)item3.Element("PHOTO")
                        }; 
                        
          result = item4.ToList();
          MapGSDAT(result, mergedb);
          return result;
        }
        ///DBMapper.MapGSDAT(mtemp.GSDAT.ToList<GSDAT>(), true);        
       public void MapGSDAT(List<GSDAT> result, bool mergedb)
        {
          if (mergedb)
            {     
            
                int loaded = 0;
                int page = 500;
                int count = result.Count();
                while (true)
                {
                    if (count - loaded < page) 
                    {
                        page = count - loaded;
                    }
                    var query =
                        (from GSDAT row in result.AsQueryable()
                         select row).Skip(loaded).Take(page);
               var rowsexist = from u in LocalDB.GSDAT.ToList().AsQueryable()
                                           join uc in query.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.GSDAT.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.GSDAT.InsertAllOnSubmit(query);
                LocalDB.SubmitChanges();
                 loaded += page;
                if (count == loaded)
                        break;
                }
            }
       }
    }
    [Table(Name = "GSDAT")]
    public class GSDAT : TBBASE
    {
        private string _MAINKEY = "0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    _MAINKEY = value;
                    NotifyPropertyChanging("MAINKEY");                 
                }
            }
        }
        private string _Photo = "YES";
        [Column]
        public string PHOTO
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    _Photo = value;
                    NotifyPropertyChanging("PHOTO");
                }
            }
        } 
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _GSMANDANT="0";
        [Column]
        public string GSMANDANT
        {
            get
            {
                return _GSMANDANT;
            }
            set
            {
                if (value != _GSMANDANT)
                {
                     _GSMANDANT = value;
                    NotifyPropertyChanged("GSMANDANT");
                }
            }
        }
        private string  _GSART="0";
        [Column]
        public string GSART
        {
            get
            {
                return _GSART;
            }
            set
            {
                if (value != _GSART)
                {
                     _GSART = value;
                    NotifyPropertyChanged("GSART");
                }
            }
        }
        private string  _GSVAR="0";
        [Column]
        public string GSVAR
        {
            get
            {
                return _GSVAR;
            }
            set
            {
                if (value != _GSVAR)
                {
                     _GSVAR = value;
                    NotifyPropertyChanged("GSVAR");
                }
            }
        }
        private string  _GSHANDLNR="0";
        [Column]
        public string GSHANDLNR
        {
            get
            {
                return _GSHANDLNR;
            }
            set
            {
                if (value != _GSHANDLNR)
                {
                     _GSHANDLNR = value;
                    NotifyPropertyChanged("GSHANDLNR");
                }
            }
        }
        private string  _GSGARNBEZ="0";
        [Column]
        public string GSGARNBEZ
        {
            get
            {
                return _GSGARNBEZ;
            }
            set
            {
                if (value != _GSGARNBEZ)
                {
                     _GSGARNBEZ = value;
                    NotifyPropertyChanged("GSGARNBEZ");
                }
            }
        }
        private string  _GSNUMART="0";
        [Column]
        public string GSNUMART
        {
            get
            {
                return _GSNUMART;
            }
            set
            {
                if (value != _GSNUMART)
                {
                     _GSNUMART = value;
                    NotifyPropertyChanged("GSNUMART");
                }
            }
        }
        private string  _GSGARNNR="0";
        [Column]
        public string GSGARNNR
        {
            get
            {
                return _GSGARNNR;
            }
            set
            {
                if (value != _GSGARNNR)
                {
                     _GSGARNNR = value;
                    NotifyPropertyChanged("GSGARNNR");
                }
            }
        }
        private string  _GSFACH="0";
        [Column]
        public string GSFACH
        {
            get
            {
                return _GSFACH;
            }
            set
            {
                if (value != _GSFACH)
                {
                     _GSFACH = value;
                    NotifyPropertyChanged("GSFACH");
                }
            }
        }
        private string  _GSRECHNUM="0";
        [Column]
        public string GSRECHNUM
        {
            get
            {
                return _GSRECHNUM;
            }
            set
            {
                if (value != _GSRECHNUM)
                {
                     _GSRECHNUM = value;
                    NotifyPropertyChanged("GSRECHNUM");
                }
            }
        }
        private string  _GSRECHNR="0";
        [Column]
        public string GSRECHNR
        {
            get
            {
                return _GSRECHNR;
            }
            set
            {
                if (value != _GSRECHNR)
                {
                     _GSRECHNR = value;
                    NotifyPropertyChanged("GSRECHNR");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<GSDAT> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\GSDAT\\GSDAT" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\GSDAT" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("GSDAT");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (GSDAT row in ListInput)
                {
                    XElement f = new XElement("GSDAT");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement GSMANDANT = new XElement("GSMANDANT");
                         GSMANDANT.Value = row.GSMANDANT;
                          f.Add(GSMANDANT);
                        XElement GSART = new XElement("GSART");
                         GSART.Value = row.GSART;
                          f.Add(GSART);
                        XElement GSVAR = new XElement("GSVAR");
                         GSVAR.Value = row.GSVAR;
                          f.Add(GSVAR);
                        XElement GSHANDLNR = new XElement("GSHANDLNR");
                         GSHANDLNR.Value = row.GSHANDLNR;
                          f.Add(GSHANDLNR);
                        XElement GSGARNBEZ = new XElement("GSGARNBEZ");
                         GSGARNBEZ.Value = row.GSGARNBEZ;
                          f.Add(GSGARNBEZ);
                        XElement GSNUMART = new XElement("GSNUMART");
                         GSNUMART.Value = row.GSNUMART;
                          f.Add(GSNUMART);
                        XElement GSGARNNR = new XElement("GSGARNNR");
                         GSGARNNR.Value = row.GSGARNNR;
                          f.Add(GSGARNNR);
                        XElement GSFACH = new XElement("GSFACH");
                         GSFACH.Value = row.GSFACH;
                          f.Add(GSFACH);
                        XElement GSRECHNUM = new XElement("GSRECHNUM");
                         GSRECHNUM.Value = row.GSRECHNUM;
                          f.Add(GSRECHNUM);
                        XElement GSRECHNR = new XElement("GSRECHNR");
                         GSRECHNR.Value = row.GSRECHNR;
                          f.Add(GSRECHNR);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }                                        
    }
}
